<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="本地端口" prop="localPortId">
        <el-input v-model="formData.localPortId" placeholder="请输入本地端口" />
      </el-form-item>
      <el-form-item label="本地设备 ID，" prop="localDeviceId">
        <el-input v-model="formData.localDeviceId" placeholder="请输入本地设备 ID，" />
      </el-form-item>
      <el-form-item label="远端端口 ID" prop="remotePortId">
        <el-input v-model="formData.remotePortId" placeholder="请输入远端端口 ID" />
      </el-form-item>
      <el-form-item label="路是否激活（1=启用，0=禁用）" prop="active">
        <el-radio-group v-model="formData.active">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="协议类型" prop="protocol">
        <el-input v-model="formData.protocol" placeholder="请输入协议类型" />
      </el-form-item>
      <el-form-item label="远端设备主机名" prop="remoteHostname">
        <el-input v-model="formData.remoteHostname" placeholder="请输入远端设备主机名" />
      </el-form-item>
      <el-form-item label="远端设备在 LibreNMS 中的 ID" prop="remoteDeviceId">
        <el-input v-model="formData.remoteDeviceId" placeholder="请输入远端设备在 LibreNMS 中的 ID" />
      </el-form-item>
      <el-form-item label="远端物理端口名称" prop="remotePort">
        <el-input v-model="formData.remotePort" placeholder="请输入远端物理端口名称" />
      </el-form-item>
      <el-form-item label="远端设备平台信息" prop="remotePlatform">
        <el-input v-model="formData.remotePlatform" placeholder="请输入远端设备平台信息" />
      </el-form-item>
      <el-form-item label="远端设备系统版本" prop="remoteVersion">
        <el-input v-model="formData.remoteVersion" placeholder="请输入远端设备系统版本" />
      </el-form-item>
      <el-form-item label="是否手动默认0自动1手动" prop="manual">
        <el-input v-model="formData.manual" placeholder="请输入是否手动默认0自动1手动" />
      </el-form-item>
      <el-form-item label="是否是关联链路0否1是" prop="isCritical">
        <el-radio-group v-model="formData.isCritical">
          <el-radio value="1">请选择字典生成</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="名称" prop="sysname">
        <el-input v-model="formData.sysname" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="画板id" prop="boardId">
        <el-input v-model="formData.boardId" placeholder="请输入画板id" />
      </el-form-item>
      <el-form-item label="警戒线" prop="warningLine">
        <el-input v-model="formData.warningLine" placeholder="请输入警戒线" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { LinksManualApi, LinksManual } from '@/api/business/linksManual'

/** 线路-手动 表单 */
defineOptions({ name: 'LinksManualForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  localPortId: undefined,
  localDeviceId: undefined,
  remotePortId: undefined,
  active: undefined,
  protocol: undefined,
  remoteHostname: undefined,
  remoteDeviceId: undefined,
  remotePort: undefined,
  remotePlatform: undefined,
  remoteVersion: undefined,
  manual: undefined,
  isCritical: undefined,
  sysname: undefined,
  boardId: undefined,
  warningLine: undefined,
})
const formRules = reactive({
  active: [{ required: true, message: '路是否激活（1=启用，0=禁用）不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await LinksManualApi.getLinksManual(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as LinksManual
    if (formType.value === 'create') {
      await LinksManualApi.createLinksManual(data)
      message.success(t('common.createSuccess'))
    } else {
      await LinksManualApi.updateLinksManual(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    localPortId: undefined,
    localDeviceId: undefined,
    remotePortId: undefined,
    active: undefined,
    protocol: undefined,
    remoteHostname: undefined,
    remoteDeviceId: undefined,
    remotePort: undefined,
    remotePlatform: undefined,
    remoteVersion: undefined,
    manual: undefined,
    isCritical: undefined,
    sysname: undefined,
    boardId: undefined,
    warningLine: undefined,
  }
  formRef.value?.resetFields()
}
</script>